<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>JavaScript | C·辰</title><meta name="author" content="辰"><meta name="copyright" content="辰"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="JavaScript">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript">
<meta property="og:url" content="http://example.com/2023/04/02/JavaScript/index.html">
<meta property="og:site_name" content="C·辰">
<meta property="og:description" content="JavaScript">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/page1.jpg">
<meta property="article:published_time" content="2023-04-02T09:04:09.000Z">
<meta property="article:modified_time" content="2023-08-16T13:18:21.983Z">
<meta property="article:author" content="辰">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/page1.jpg"><link rel="shortcut icon" href="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/Bell.ico"><link rel="canonical" href="http://example.com/2023/04/02/JavaScript/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'JavaScript',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-08-16 21:18:21'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/loading.gif" data-original="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog%2Fimages%2Favatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">32</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> Demo</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/c2c_shop/"><i class="fa-fw fa fa-comments-o"></i><span> 商城</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/page1.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="C·辰"><span class="site-name">C·辰</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> Demo</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/c2c_shop/"><i class="fa-fw fa fa-comments-o"></i><span> 商城</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">JavaScript</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-04-02T09:04:09.000Z" title="发表于 2023-04-02 17:04:09">2023-04-02</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-08-16T13:18:21.983Z" title="更新于 2023-08-16 21:18:21">2023-08-16</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/JavaScript/">JavaScript</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>7分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="JavaScript"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="基础语法"><a href="#基础语法" class="headerlink" title="基础语法"></a>基础语法</h1><h5 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//使用常量(**const**)或者变量(**let**)</span></span><br><span class="line"><span class="keyword">let</span> 变量名</span><br><span class="line"><span class="keyword">const</span> 常量</span><br><span class="line"><span class="keyword">var</span> 可重复定义</span><br></pre></td></tr></table></figure>

<br/>

<h5 id="警示框"><a href="#警示框" class="headerlink" title="警示框"></a>警示框</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//多个输出使用 +</span></span><br><span class="line"><span class="title function_">alter</span>(<span class="string">&#x27;&#x27;</span>) </span><br></pre></td></tr></table></figure>

<br/>

<h5 id="文档输出"><a href="#文档输出" class="headerlink" title="文档输出"></a>文档输出</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="variable language_">document</span>.<span class="title function_">write</span>(<span class="string">&#x27;注入这个内容&#x27;</span>)</span><br></pre></td></tr></table></figure>

<br/>

<h5 id="控制台打印输出"><a href="#控制台打印输出" class="headerlink" title="控制台打印输出"></a>控制台打印输出</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;在控制台输出信息&#x27;</span>)</span><br></pre></td></tr></table></figure>

<br/>

<h5 id="输入语句"><a href="#输入语句" class="headerlink" title="输入语句"></a>输入语句</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="title function_">prompt</span>(<span class="string">&#x27;提示输入的文本&#x27;</span>) </span><br></pre></td></tr></table></figure>

<br/>

<h5 id="数组Array"><a href="#数组Array" class="headerlink" title="数组Array"></a>数组Array</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="title function_">arr</span>(数组名)=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>] </span><br><span class="line"></span><br><span class="line"><span class="comment">//末尾新增(新增在数组末尾并返回新的长度) </span></span><br><span class="line">	arr.<span class="title function_">push</span>()</span><br><span class="line"></span><br><span class="line"><span class="comment">//开头新增(新增在数组开头并返回新的长度)</span></span><br><span class="line">	arr.<span class="title function_">unshift</span>()</span><br><span class="line"></span><br><span class="line"><span class="comment">//删除(删除最后一个元素) </span></span><br><span class="line">    arr.<span class="property">pop</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//删除(删除第一个元素) </span></span><br><span class="line">	arr.<span class="property">shift</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//删除(指定元素)  splice(起始位置，删除几个元素)</span></span><br><span class="line">	arr.<span class="title function_">splice</span>(<span class="number">0</span>,<span class="number">10</span>)</span><br></pre></td></tr></table></figure>

<br/>

<h5 id="模板字符串"><a href="#模板字符串" class="headerlink" title="模板字符串"></a>模板字符串</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//使用反引号，值使用 $&#123;&#125;</span></span><br><span class="line"><span class="string">`这个值为：<span class="subst">$&#123;x&#125;</span>`</span></span><br></pre></td></tr></table></figure>

<br/>

<h5 id="检测数据类型"><a href="#检测数据类型" class="headerlink" title="检测数据类型"></a>检测数据类型</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//typeof +变量</span></span><br><span class="line"><span class="keyword">let</span> c=<span class="number">99</span></span><br><span class="line">consle.<span class="title function_">log</span>(<span class="keyword">typeof</span> c)</span><br></pre></td></tr></table></figure>

<br/>

<h5 id="类型转换"><a href="#类型转换" class="headerlink" title="类型转换"></a>类型转换</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//隐式转换</span></span><br><span class="line">    <span class="comment">// + 号一边为字符型则输出为字符型</span></span><br><span class="line">    <span class="comment">// - * / 都转为数字类型number</span></span><br><span class="line">    <span class="keyword">let</span> number= + <span class="title function_">prompt</span>(<span class="string">&#x27;输入字符串转为数字&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//显示转换</span></span><br><span class="line">    <span class="comment">//转成数字</span></span><br><span class="line">    number.<span class="title class_">Number</span>()</span><br><span class="line">	number.<span class="title function_">parse</span>()</span><br><span class="line"></span><br><span class="line">    <span class="comment">//转成字符串：String</span></span><br><span class="line">	number.<span class="title function_">toString</span>()</span><br></pre></td></tr></table></figure>

<br/>

<h5 id="保留整数"><a href="#保留整数" class="headerlink" title="保留整数"></a>保留整数</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//只保留字符前面出现的整数</span></span><br><span class="line"><span class="keyword">let</span> c=<span class="string">&#x27;123abc&#x27;</span></span><br><span class="line"><span class="built_in">parseInt</span>(c) -----&gt;<span class="number">123</span></span><br></pre></td></tr></table></figure>

<br/>

<h5 id="保留小数及整数"><a href="#保留小数及整数" class="headerlink" title="保留小数及整数"></a>保留小数及整数</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> c=<span class="string">&#x27;123.123abc&#x27;</span></span><br><span class="line"><span class="built_in">parseFloat</span>(c) ------&gt;<span class="number">123.123</span></span><br></pre></td></tr></table></figure>

<br/>

<h5 id="函数"><a href="#函数" class="headerlink" title="函数"></a><strong>函数</strong></h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">function</span> 函数名()&#123;</span><br><span class="line">    函数体</span><br><span class="line">    <span class="keyword">return</span> 数值</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">chen</span>(<span class="params">x,y</span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> x+y</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<br/>

<h5 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> 名称=<span class="keyword">function</span>(<span class="params"></span>)&#123; </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> chen=<span class="keyword">function</span>(<span class="params">c,p</span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> c+p</span><br><span class="line">&#125;</span><br><span class="line">    </span><br></pre></td></tr></table></figure>

<br/>

<h5 id="立即函数"><a href="#立即函数" class="headerlink" title="立即函数"></a>立即函数</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">(<span class="title function_">fuction</span>(<span class="params"></span>)&#123; &#125;)()</span><br><span class="line"></span><br><span class="line">(<span class="keyword">function</span>(<span class="params">x,y</span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> x+y</span><br><span class="line">&#125;)(<span class="number">1</span>,<span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<br/>

<h5 id="对象"><a href="#对象" class="headerlink" title="对象"></a>对象</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> 对象名 =&#123; &#125;    </span><br><span class="line"><span class="keyword">let</span> 对象名 = <span class="keyword">new</span> <span class="title class_">Object</span>()</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> chen=&#123;</span><br><span class="line">    <span class="attr">name</span>:<span class="string">&#x27;peng&#x27;</span>,</span><br><span class="line">    <span class="attr">age</span>:<span class="number">18</span>,</span><br><span class="line">    <span class="attr">where</span>:<span class="string">&quot;广东&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 查看</span></span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(chen.<span class="property">name</span>)</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(chen[<span class="string">&#x27;name&#x27;</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment">//新增--修改</span></span><br><span class="line">        chen.<span class="property">how</span>=<span class="string">&#x27;这是第一次出现则为新增，否则为修改&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 删除</span></span><br><span class="line">        <span class="keyword">delete</span> chen.<span class="property">where</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//遍历输出 -- for(let 变量 in 对象)</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">let</span> i <span class="keyword">in</span> chen)&#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(chen[i]);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//遍历输出--Object.keys(对象)</span></span><br><span class="line">		<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span>.<span class="title function_">keys</span>(chen))</span><br><span class="line"></span><br><span class="line">		</span><br><span class="line"><span class="comment">//对象中使用方法</span></span><br><span class="line">	<span class="keyword">let</span> obj =&#123;</span><br><span class="line">            <span class="attr">name</span>:<span class="string">&#x27;chen&#x27;</span>,</span><br><span class="line">            <span class="attr">song</span>:<span class="keyword">function</span>(<span class="params">x,y</span>)&#123;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<br/>

<br/>

<hr>
<br/>

<br/>

<h1 id="Web-apis"><a href="#Web-apis" class="headerlink" title="Web-apis"></a>Web-apis</h1><h2 id="①选择器"><a href="#①选择器" class="headerlink" title="①选择器"></a>①<strong>选择器</strong></h2><h5 id="获取第一个匹配的元素"><a href="#获取第一个匹配的元素" class="headerlink" title="获取第一个匹配的元素"></a>获取第一个匹配的元素</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//querySelector</span></span><br><span class="line"><span class="variable language_">document</span>.<span class="title function_">querySelector</span>(<span class="string">&#x27;css选择器&#x27;</span>)   </span><br><span class="line"><span class="variable language_">document</span>.<span class="title function_">querySelector</span>(<span class="string">&#x27;ul(上层) li(本层)&#x27;</span>)   </span><br><span class="line"><span class="keyword">const</span> box=<span class="variable language_">document</span>.<span class="title function_">querySelector</span>(<span class="string">&#x27;div&#x27;</span>)</span><br></pre></td></tr></table></figure>

<br/>

<h5 id="通过ID名选择"><a href="#通过ID名选择" class="headerlink" title="通过ID名选择"></a>通过ID名选择</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> idName=<span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&#x27;id_name&#x27;</span>)</span><br><span class="line">idName.<span class="property">style</span>.<span class="property">color</span>=<span class="string">&#x27;green&#x27;</span></span><br><span class="line">idName.<span class="property">style</span>.<span class="property">fontSize</span>=<span class="string">&#x27;100px&#x27;</span></span><br></pre></td></tr></table></figure>

<br/>

<h5 id="通过Class名选择"><a href="#通过Class名选择" class="headerlink" title="通过Class名选择"></a>通过Class名选择</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//通过Class名获取元素--返回的为数组</span></span><br><span class="line"><span class="keyword">var</span> className=<span class="variable language_">document</span>.<span class="title function_">getElementsByClassName</span>(<span class="string">&quot;class_name&quot;</span>)[<span class="number">0</span>]</span><br><span class="line">className.<span class="property">style</span>.<span class="property">fontSize</span>=<span class="string">&#x27;100px&#x27;</span></span><br></pre></td></tr></table></figure>

<br/>

<h5 id="获取所有匹配的元素"><a href="#获取所有匹配的元素" class="headerlink" title="获取所有匹配的元素"></a>获取所有匹配的元素</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// querySelectorall --获取过来的为数组（**class******名**或**id**名**或元素名都可）</span></span><br><span class="line"><span class="keyword">const</span> nav=<span class="variable language_">document</span>.<span class="title function_">querySelectorAll</span>(<span class="string">&#x27;.nav li&#x27;</span>)</span><br></pre></td></tr></table></figure>



<ul>
<li>（旧方法）</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//通过id获取第一个元素</span></span><br><span class="line">   <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&#x27;hhc&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//根据标签获取一类元素 获取页面所有的div</span></span><br><span class="line">		<span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment">//根据类名class获取元素 </span></span><br><span class="line">        <span class="variable language_">document</span>.<span class="title function_">getElementsByClassName</span>(<span class="string">&#x27;class&#x27;</span>)</span><br></pre></td></tr></table></figure>

<br/>

<h5 id="修改属性"><a href="#修改属性" class="headerlink" title="修改属性"></a>修改属性</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> image=<span class="variable language_">document</span>.<span class="title function_">querySelector</span>(<span class="string">&#x27;img&#x27;</span>)</span><br><span class="line">image.<span class="property">src</span>=<span class="string">&#x27;../../html/images/2.jpg&#x27;</span></span><br></pre></td></tr></table></figure>

<br/>

<h5 id="修改样式"><a href="#修改样式" class="headerlink" title="修改样式"></a>修改样式</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//--调用.style</span></span><br><span class="line"><span class="comment">//（多组单词使用小驼峰命名法）css中:background-color</span></span><br><span class="line">变量.<span class="property">style</span>.<span class="property">backgroundColor</span>=<span class="string">&#x27;blue&#x27;</span></span><br></pre></td></tr></table></figure>

<br/>

<h5 id="classlist类名操作"><a href="#classlist类名操作" class="headerlink" title="classlist类名操作"></a>classlist类名操作</h5><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//(添加类名/删除类名)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//添加</span></span><br><span class="line">元素.<span class="property">classList</span>.<span class="title function_">add</span>(<span class="string">&#x27;第二个类名&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//删除</span></span><br><span class="line">元素.<span class="property">classList</span>.<span class="title function_">remove</span>(<span class="string">&#x27;类名&#x27;</span>)</span><br></pre></td></tr></table></figure>



<br/>

<br/>

<hr>
<br/>

<br/>

<h1 id="axios"><a href="#axios" class="headerlink" title="axios"></a>axios</h1><h2 id="发送数据"><a href="#发送数据" class="headerlink" title="发送数据"></a>发送数据</h2><blockquote>
<p>无参：<strong>axios.get&#x2F;post(url).then(成功).catch(错误).finally()</strong></p>
<p>有参：<strong>axios.get(url,,{params:{}}).then(成功).catch(错误).finally()</strong></p>
<p>then:一个函数，表示请求成功时执行的</p>
<p>resp 是形参，表示服务器返回的数据，这个数据是由axios封装好的</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">     	<span class="comment">/* ******************************************************get****************************************************** */</span></span></span><br><span class="line"><span class="language-javascript">		<span class="comment">/* get请求没有参数 */</span></span></span><br><span class="line"><span class="language-javascript">		<span class="keyword">function</span> <span class="title function_">funGet</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">		</span></span><br><span class="line"><span class="language-javascript">			<span class="keyword">let</span> url = <span class="string">&quot;http://localhost:9000/user/query&quot;</span></span></span><br><span class="line"><span class="language-javascript">			<span class="comment">//箭头函数（省略定义函数，不用函数名）： 函数 （参数） &#123;函数体&#125;   ------&gt;  (参数)=&gt;&#123;函数体&#125;</span></span></span><br><span class="line"><span class="language-javascript">			axios.<span class="title function_">get</span>(url).<span class="title function_">then</span>( <span class="function"><span class="params">resp</span> =&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="comment">//then 是一个函数，表示请求成功时执行的</span></span></span><br><span class="line"><span class="language-javascript">				<span class="comment">//resp 是形参，表示服务器返回的数据，这个数据是由axios封装好的</span></span></span><br><span class="line"><span class="language-javascript">				<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求成功，返回服务器的数据：&quot;</span>+resp);</span></span><br><span class="line"><span class="language-javascript">			&#125;).<span class="title function_">catch</span>(<span class="function"><span class="params">err</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求错误&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">			&#125;).<span class="title function_">finally</span>(<span class="function">()=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="comment">//console.log(&quot;finally&quot;)</span></span></span><br><span class="line"><span class="language-javascript">			&#125;)</span></span><br><span class="line"><span class="language-javascript">		&#125;</span></span><br><span class="line"><span class="language-javascript">		</span></span><br><span class="line"><span class="language-javascript">    		 <span class="comment">/* get请求路径携带参数 */</span></span></span><br><span class="line"><span class="language-javascript">		<span class="keyword">function</span> <span class="title function_">Get</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">			<span class="keyword">let</span> url = <span class="string">&quot;http://localhost:9000/user/get?id=1002&amp;name=彭呀&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">			axios.<span class="title function_">get</span>(url).<span class="title function_">then</span>( <span class="function"><span class="params">resp</span> =&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求成功，返回服务器的数据：&quot;</span>+resp);</span></span><br><span class="line"><span class="language-javascript">			&#125;).<span class="title function_">catch</span>(<span class="function"><span class="params">err</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求错误&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">			&#125;)</span></span><br><span class="line"><span class="language-javascript">		&#125;</span></span><br><span class="line"><span class="language-javascript">		</span></span><br><span class="line"><span class="language-javascript">     </span></span><br><span class="line"><span class="language-javascript">    		<span class="comment">/* get请求外置参数 */</span></span></span><br><span class="line"><span class="language-javascript">		<span class="keyword">function</span> <span class="title function_">Get2</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">			<span class="keyword">let</span> url = <span class="string">&quot;http://localhost:9000/user/get&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">			axios.<span class="title function_">get</span>(url,&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="attr">params</span>:&#123;</span></span><br><span class="line"><span class="language-javascript">					<span class="attr">id</span>:<span class="number">3000</span>,</span></span><br><span class="line"><span class="language-javascript">					<span class="attr">name</span>:<span class="string">&#x27;chen同学&#x27;</span></span></span><br><span class="line"><span class="language-javascript">				&#125;</span></span><br><span class="line"><span class="language-javascript">			&#125;).<span class="title function_">then</span>(<span class="function"><span class="params">resp</span> =&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求成功，返回服务器的数据：&quot;</span>+resp);</span></span><br><span class="line"><span class="language-javascript">			&#125;)</span></span><br><span class="line"><span class="language-javascript">			.<span class="title function_">catch</span>(<span class="function"><span class="params">err</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求错误&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">			&#125;)</span></span><br><span class="line"><span class="language-javascript">		&#125;</span></span><br><span class="line"><span class="language-javascript">     </span></span><br><span class="line"><span class="language-javascript">          <span class="comment">/* ******************************************************post****************************************************** */</span></span></span><br><span class="line"><span class="language-javascript">     	<span class="comment">//post请求，传递参数格式  ：参数名=值&amp;参数名=值</span></span></span><br><span class="line"><span class="language-javascript">		<span class="keyword">function</span> <span class="title function_">Post</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">			<span class="keyword">let</span> url = <span class="string">&quot;http://localhost:9000/user/add&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">			<span class="comment">//Post(&quot;url&quot;,&quot;参数名=值&amp;参数名=值&quot;)</span></span></span><br><span class="line"><span class="language-javascript">			axios.<span class="title class_">Post</span>(url,<span class="string">&quot;id=12345&amp;name=彭呀&quot;</span>)</span></span><br><span class="line"><span class="language-javascript">			.<span class="title function_">then</span>(<span class="function"><span class="params">resp</span> =&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求成功，返回服务器的数据：&quot;</span>+resp);</span></span><br><span class="line"><span class="language-javascript">			&#125;)</span></span><br><span class="line"><span class="language-javascript">			.<span class="title function_">catch</span>(<span class="function"><span class="params">err</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求错误&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">			&#125;)</span></span><br><span class="line"><span class="language-javascript">		&#125;</span></span><br><span class="line"><span class="language-javascript">     		</span></span><br><span class="line"><span class="language-javascript">      	 </span></span><br><span class="line"><span class="language-javascript">     	<span class="comment">//post请求,传递的是对象数据，axios自动转为json  -- 后端 @RequestBody 映射</span></span></span><br><span class="line"><span class="language-javascript">		<span class="keyword">function</span> <span class="title function_">Post2</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">			<span class="keyword">let</span> url = <span class="string">&quot;http://localhost:9000/user/json&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">			<span class="keyword">let</span> data=&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="attr">id</span>:<span class="number">999</span>,</span></span><br><span class="line"><span class="language-javascript">				<span class="attr">name</span>:<span class="string">&quot;张三&quot;</span>,</span></span><br><span class="line"><span class="language-javascript">				<span class="attr">sex</span>:<span class="string">&quot;女&quot;</span>,</span></span><br><span class="line"><span class="language-javascript">				<span class="attr">age</span>:<span class="number">20</span></span></span><br><span class="line"><span class="language-javascript">				</span></span><br><span class="line"><span class="language-javascript">			&#125;</span></span><br><span class="line"><span class="language-javascript">			axios.<span class="title function_">post</span>(url,data)</span></span><br><span class="line"><span class="language-javascript">			.<span class="title function_">then</span>(<span class="function"><span class="params">resp</span> =&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求成功，返回服务器的数据：&quot;</span>+resp);</span></span><br><span class="line"><span class="language-javascript">			&#125;)</span></span><br><span class="line"><span class="language-javascript">			.<span class="title function_">catch</span>(<span class="function"><span class="params">err</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">				<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求错误&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">			&#125;)</span></span><br><span class="line"><span class="language-javascript">		&#125;</span></span><br><span class="line"><span class="language-javascript">     </span></span><br><span class="line"><span class="language-javascript">     </span></span><br><span class="line"><span class="language-javascript">     </span></span><br><span class="line"><span class="language-javascript">     </span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>



<ul>
<li>配置项</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//post请求,传递的是对象数据，axios自动转为json</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Post3</span>(<span class="params"></span>)&#123;</span><br><span class="line">	<span class="keyword">let</span> url = <span class="string">&quot;http://localhost:9000/user/json&quot;</span>;</span><br><span class="line">	<span class="keyword">let</span> param=&#123;</span><br><span class="line">		<span class="attr">id</span>:<span class="number">999</span>,</span><br><span class="line">		<span class="attr">name</span>:<span class="string">&quot;张辰&quot;</span>,</span><br><span class="line">		<span class="attr">sex</span>:<span class="string">&quot;女&quot;</span>,</span><br><span class="line">		<span class="attr">age</span>:<span class="number">20</span></span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">         </span><br><span class="line">	<span class="title function_">axios</span>(&#123;</span><br><span class="line">		<span class="attr">url</span>:url,</span><br><span class="line">		<span class="attr">method</span>:<span class="string">&quot;post&quot;</span>,</span><br><span class="line">		<span class="attr">data</span>:param</span><br><span class="line">	&#125;).<span class="title function_">then</span>(<span class="function"><span class="params">resp</span>=&gt;</span>&#123;</span><br><span class="line">		<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;请求成功，返回服务器的数据：&quot;</span>+resp);</span><br><span class="line">	&#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<br/>

<br/>

<h2 id="接收数据"><a href="#接收数据" class="headerlink" title="接收数据"></a>接收数据</h2><p><strong>响应结构是一个json</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  // `data` 由服务器提供的响应</span><br><span class="line">  data: &#123;&#125;,</span><br><span class="line"></span><br><span class="line">  // `status` 来自服务器响应的 HTTP 状态码</span><br><span class="line">  status: 200,</span><br><span class="line"></span><br><span class="line">  // `statusText` 来自服务器响应的 HTTP 状态信息</span><br><span class="line">  statusText: <span class="string">&#x27;OK&#x27;</span>,</span><br><span class="line"></span><br><span class="line">  // `headers` 服务器响应的头</span><br><span class="line">  headers: &#123;&#125;,</span><br><span class="line"></span><br><span class="line">  // `config` 是为请求提供的配置信息</span><br><span class="line">  config: &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<br/>

<br/>

<hr>
<br/>

<br/>

<h1 id="拦截器"><a href="#拦截器" class="headerlink" title="拦截器"></a>拦截器</h1><ul>
<li>axios.interceptors.request.use(function(拦截){  操作 return 拦截}）</li>
</ul>
<p><img src= "" data-lazy-src="/img/loading.gif" data-original="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/axios.png" alt="axios"></p>
<br/>

<br/>

<h2 id="请求拦截器"><a href="#请求拦截器" class="headerlink" title="请求拦截器"></a>请求拦截器</h2><p>请求拦截器：在发起请求之前执行，可以对请求内容做修改，比如增加参数，设置请求头等等</p>
<ul>
<li>config</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* 请求拦截器 */</span></span><br><span class="line">axios.<span class="property">interceptors</span>.<span class="property">request</span>.<span class="title function_">use</span>(<span class="keyword">function</span>(<span class="params">config</span>)&#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;对请求config数据进行附加操作&quot;</span>)</span><br><span class="line">             </span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;url=&quot;</span>+config.<span class="property">url</span>);</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;methods：&quot;</span>+config.<span class="property">method</span>);</span><br><span class="line">	config.<span class="property">url</span> = config.<span class="property">url</span>+<span class="string">&quot;?token=xxx&quot;</span></span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;url=&quot;</span>+config.<span class="property">url</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/* 返回处理后的请求数据进行请求操作 */</span></span><br><span class="line">	<span class="keyword">return</span> config;</span><br><span class="line">	</span><br><span class="line">&#125;,<span class="keyword">function</span>(<span class="params">err</span>)&#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(err);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<br/>

<br/>

<hr>
<br/>

<br/>

<h2 id="应答拦截器"><a href="#应答拦截器" class="headerlink" title="应答拦截器"></a>应答拦截器</h2><p>应答拦截器：服务器返回结果，axios的<strong>then之前</strong>先执行。可以对应答内容对预先的处理。</p>
<ul>
<li>response</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* 应答拦截器 */</span></span><br><span class="line">axios.<span class="property">interceptors</span>.<span class="property">response</span>.<span class="title function_">use</span>(<span class="keyword">function</span>(<span class="params">response</span>)&#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;应答拦截器，接收服务器返回的数据结构&quot;</span>);</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(response);</span><br><span class="line">	<span class="keyword">return</span> response;</span><br><span class="line">&#125;,<span class="keyword">function</span>(<span class="params">err</span>)&#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(err);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://example.com">辰</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2023/04/02/JavaScript/">http://example.com/2023/04/02/JavaScript/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">C·辰</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaScript/">JavaScript</a></div><div class="post_share"><div class="social-share" data-image="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/page1.jpg" data-sites="wechat,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/04/01/Docker/" title="Git&amp;idea"><img class="cover" src= "" data-lazy-src="/img/loading.gif" data-original="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/docker.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Git&amp;idea</div></div></a></div><div class="next-post pull-right"><a href="/2023/04/05/Vue-%E5%9F%BA%E7%A1%80/" title="Vue-基础"><img class="cover" src= "" data-lazy-src="/img/loading.gif" data-original="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/Vue.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Vue-基础</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/img/loading.gif" data-original="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog%2Fimages%2Favatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">辰</div><div class="author-info__description">车遥马慢</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">32</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/hchenp"><i class="fab fa-github"></i><span>Gitee-HchenP</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/hchenp" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://github.com/Pxlchen?tab=projects" target="_blank" title="CSDN"><i class="fa fa-book-open"></i></a><a class="social-icon" href="https://console.cloud.tencent.com/cos/bucket?bucket=chen-1317386995&amp;region=ap-guangzhou&amp;isPublic=true" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="mailto:1107383160@qq.com" target="_blank" title="Email"><i class="fas fa-envelope-open-text"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">C2C-Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95"><span class="toc-number">1.</span> <span class="toc-text">基础语法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%8F%98%E9%87%8F"><span class="toc-number">1.0.0.0.1.</span> <span class="toc-text">变量</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%AD%A6%E7%A4%BA%E6%A1%86"><span class="toc-number">1.0.0.0.2.</span> <span class="toc-text">警示框</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%87%E6%A1%A3%E8%BE%93%E5%87%BA"><span class="toc-number">1.0.0.0.3.</span> <span class="toc-text">文档输出</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8E%A7%E5%88%B6%E5%8F%B0%E6%89%93%E5%8D%B0%E8%BE%93%E5%87%BA"><span class="toc-number">1.0.0.0.4.</span> <span class="toc-text">控制台打印输出</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%BE%93%E5%85%A5%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.0.0.0.5.</span> <span class="toc-text">输入语句</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%95%B0%E7%BB%84Array"><span class="toc-number">1.0.0.0.6.</span> <span class="toc-text">数组Array</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%A8%A1%E6%9D%BF%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">1.0.0.0.7.</span> <span class="toc-text">模板字符串</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%A3%80%E6%B5%8B%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.0.0.0.8.</span> <span class="toc-text">检测数据类型</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.0.0.0.9.</span> <span class="toc-text">类型转换</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%9D%E7%95%99%E6%95%B4%E6%95%B0"><span class="toc-number">1.0.0.0.10.</span> <span class="toc-text">保留整数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%9D%E7%95%99%E5%B0%8F%E6%95%B0%E5%8F%8A%E6%95%B4%E6%95%B0"><span class="toc-number">1.0.0.0.11.</span> <span class="toc-text">保留小数及整数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.0.0.12.</span> <span class="toc-text">函数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.0.0.13.</span> <span class="toc-text">匿名函数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%AB%8B%E5%8D%B3%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.0.0.14.</span> <span class="toc-text">立即函数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.0.0.0.15.</span> <span class="toc-text">对象</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Web-apis"><span class="toc-number">2.</span> <span class="toc-text">Web-apis</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E2%91%A0%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">2.1.</span> <span class="toc-text">①选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%8C%B9%E9%85%8D%E7%9A%84%E5%85%83%E7%B4%A0"><span class="toc-number">2.1.0.0.1.</span> <span class="toc-text">获取第一个匹配的元素</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%80%9A%E8%BF%87ID%E5%90%8D%E9%80%89%E6%8B%A9"><span class="toc-number">2.1.0.0.2.</span> <span class="toc-text">通过ID名选择</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%80%9A%E8%BF%87Class%E5%90%8D%E9%80%89%E6%8B%A9"><span class="toc-number">2.1.0.0.3.</span> <span class="toc-text">通过Class名选择</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E5%8C%B9%E9%85%8D%E7%9A%84%E5%85%83%E7%B4%A0"><span class="toc-number">2.1.0.0.4.</span> <span class="toc-text">获取所有匹配的元素</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E5%B1%9E%E6%80%A7"><span class="toc-number">2.1.0.0.5.</span> <span class="toc-text">修改属性</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E6%A0%B7%E5%BC%8F"><span class="toc-number">2.1.0.0.6.</span> <span class="toc-text">修改样式</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#classlist%E7%B1%BB%E5%90%8D%E6%93%8D%E4%BD%9C"><span class="toc-number">2.1.0.0.7.</span> <span class="toc-text">classlist类名操作</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#axios"><span class="toc-number">3.</span> <span class="toc-text">axios</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%91%E9%80%81%E6%95%B0%E6%8D%AE"><span class="toc-number">3.1.</span> <span class="toc-text">发送数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8E%A5%E6%94%B6%E6%95%B0%E6%8D%AE"><span class="toc-number">3.2.</span> <span class="toc-text">接收数据</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="toc-number">4.</span> <span class="toc-text">拦截器</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AF%B7%E6%B1%82%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="toc-number">4.1.</span> <span class="toc-text">请求拦截器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BA%94%E7%AD%94%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="toc-number">4.2.</span> <span class="toc-text">应答拦截器</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/07/06/Java%E5%85%AB%E8%82%A1%E6%96%87/" title="Java八股文"><img src= "" data-lazy-src="/img/loading.gif" data-original="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/javaall.gif" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java八股文"/></a><div class="content"><a class="title" href="/2023/07/06/Java%E5%85%AB%E8%82%A1%E6%96%87/" title="Java八股文">Java八股文</a><time datetime="2023-07-06T14:22:22.000Z" title="发表于 2023-07-06 22:22:22">2023-07-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/07/06/%E5%90%8E%E7%AB%AF%E8%A7%84%E5%88%92/" title="Java后端"><img src= "" data-lazy-src="/img/loading.gif" data-original="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/javahd.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java后端"/></a><div class="content"><a class="title" href="/2023/07/06/%E5%90%8E%E7%AB%AF%E8%A7%84%E5%88%92/" title="Java后端">Java后端</a><time datetime="2023-07-06T14:22:22.000Z" title="发表于 2023-07-06 22:22:22">2023-07-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/06/02/MybatisPlus/" title="MybatisPlus"><img src= "" data-lazy-src="/img/loading.gif" data-original="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/mybatisplus.svg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="MybatisPlus"/></a><div class="content"><a class="title" href="/2023/06/02/MybatisPlus/" title="MybatisPlus">MybatisPlus</a><time datetime="2023-06-02T14:22:22.000Z" title="发表于 2023-06-02 22:22:22">2023-06-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/05/05/Linux/" title="Linux"><img src= "" data-lazy-src="/img/loading.gif" data-original="https://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/Linux.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Linux"/></a><div class="content"><a class="title" href="/2023/05/05/Linux/" title="Linux">Linux</a><time datetime="2023-05-05T14:22:22.000Z" title="发表于 2023-05-05 22:22:22">2023-05-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/05/05/Redis/" title="Redis"><img src= "" data-lazy-src="/img/loading.gif" data-original="ttps://chen-1317386995.cos.ap-guangzhou.myqcloud.com/Blog/images/Redis.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Redis"/></a><div class="content"><a class="title" href="/2023/05/05/Redis/" title="Redis">Redis</a><time datetime="2023-05-05T14:22:22.000Z" title="发表于 2023-05-05 22:22:22">2023-05-05</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 辰</div><div class="framework-info"><a target="_blank" rel="noopener" href="https://beian.miit.gov.cn">粤ICP备2023096650号</a><span class="footer-separator">|</span><a target="_blank" rel="noopener" href="http://hchen.cloud">hchen.cloud</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div>
        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(r){r.imageLazyLoadSetting.processImages=t;var e=r.imageLazyLoadSetting.isSPA,n=r.imageLazyLoadSetting.preloadRatio||1,c=a();function a(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){e&&(c=a());for(var t,o=0;o<c.length;o++)0<=(t=(t=c[o]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(r.innerHeight*n||document.documentElement.clientHeight*n)&&function(){var t,e,n,a,i=c[o];e=function(){c=c.filter(function(t){return i!==t}),r.imageLazyLoadSetting.onImageLoaded&&r.imageLazyLoadSetting.onImageLoaded(i)},(t=i).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,a=t.getAttribute("data-original"),n.onload=function(){t.src=a,t.removeAttribute("data-original"),e&&e()},t.src!==a&&(n.src=a))}()}function i(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",i),r.addEventListener("resize",i),r.addEventListener("orientationchange",i)}(this);</script></body></html>